let utils = require("./utils.js");
copyUrl = utils.getClip();


headers = {
    "headers": { 'user-agent': 'GDTADNetClient-[Dalvik/2.1.0 (Linux; U; Android 10; PCT-AL10 Build/HUAWEIPCT-AL10)]' }
}
function getLocation(copyUrl) {
    copyUrl = copyUrl||"https://v.douyin.com/JCwoshW/";
    pat = /[a-zA-z]+:\/\/[^\s]*/;
    var url = pat.exec(copyUrl);
    log("url:   " + url)
    
    var r = http.get(String(url), headers, function (params) {
        pat = /\/video\/(\d+)\//;
        var id = pat.exec(params.url);
        if (id != null) {
            video_id = String(id[1]);
            new_url = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + video_id;
            resp = http.get(new_url, headers).body.json();
            // log(rso.item_list[0])
            playUrl = resp["item_list"][0]["video"]["play_addr"]["url_list"][0].replace('playwm', 'play');
            title = resp["item_list"][0]["desc"];
            console.log("title" + title);
            downUrl = http.get(playUrl, headers).url;
            log("download       " + downUrl)
            res = http.get(String(downUrl), headers);
            console.log("res.statusCode " + res.statusCode);
            if (res.statusCode != 200) {
                toastLog("下载失败！");
            } else {
                files.writeBytes("/sdcard/DCIM/Camera/" + title + ".mp4", res.body.bytes());
                toastLog("下载成功！");
            }
        }
        
    });
}


getLocation(copyUrl);